package com.wow.wa.bill.util;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;

import android.util.Log;

import com.wow.wa.bill.serivce.vo.DateRange;
import com.wow.wa.bill.vo.Bill;
import com.wow.wa.util.CommonUtils;

public class BilComponetImpl implements BilComponet {

	private static final String TAG = "BilComponet";

//	@Override
//	public List<Bill> createManyBill(Bill templateBill, String period,
//			String amount, String eacdayOfMon, String percentage) {
//
//		Long iAmount = this.getPeriodBill(amount, percentage, period);// 總資金
//		Long everyBillMoney = this.getPeriodBill(iAmount.toString(), period);
//
//		List<Bill> bills = new ArrayList<Bill>();
//		Integer time = Integer.parseInt(period);
//		try {
//
//			String yyymmdd = templateBill.getDueDate();
//			for (int i = 0; i < time; i++) {
//
//				Bill bill = this.getColonBill(templateBill);
//
//				bill.setDollar(everyBillMoney.toString());
//				bill.setDueDate(yyymmdd);
//
//				yyymmdd = this.getNextDateBy(yyymmdd);
//				bills.add(bill);
//
//			}
//		} catch (Exception e) {
//			Log.e(TAG, "error={}" + e);
//		}
//
//		return bills;
//	}

//	private Bill getColonBill(Bill templateBill) {
//		Bill bill = new Bill();
//
//		bill.setCategory(templateBill.getCategoryId());
//		bill.setCreateDate(templateBill.getCreateDate());
//		bill.setIsPayed(templateBill.getIsPayed());
//		bill.setName(templateBill.getName());
//
//		bill.setIsShowPayed("N");
//		bill.setDollar(templateBill.getDollar());
//		bill.setDueDate(templateBill.getDueDate());
//		return bill;
//	}

	@Override
	public Long getAmount(String amount, String percentage) {

		return (long) (Double.parseDouble(amount) + Double.parseDouble(amount)
				* Double.parseDouble(percentage) / 100);// 總資金
	}

	@Override
	public Long getPeriodBill(String amount, String period) {

		return this.getPeriodBill(amount, "1", period);
	}

	@Override
	public Long getPeriodBill(String amount, String percentage, String period) {

		return (Long.parseLong(amount) * Long.parseLong(percentage))
				/ Integer.parseInt(period);
	}

	@Override
	public String getNextDateBy(String yyymmdd) {

		try {
			Calendar cal = Calendar.getInstance();
			Date date = org.apache.commons.lang3.time.DateUtils.parseDate(
					yyymmdd, CommonUtils.yyymmddFormat);
			cal.setTime(date);
			cal.add(Calendar.MONTH, -1);

			return CommonUtils.getDate(cal.getTime());

		} catch (Exception e) {
			Log.e(TAG, "error=" + e);
		}

		return "";
	}

	@Override
	public String getBeforeDateBy(String yyymmdd) {

		try {
			Calendar cal = Calendar.getInstance();
			Date date = org.apache.commons.lang3.time.DateUtils.parseDate(
					yyymmdd, CommonUtils.yyymmddFormat);
			cal.setTime(date);
			cal.add(Calendar.MONTH, +1);

			return CommonUtils.getDate(cal.getTime());

		} catch (Exception e) {
			Log.e(TAG, "error=" + e);
		}

		return "";
	}

	@Override
	public String getNextDateBy(String yyymmdd, String dayOfPeriod) {

		try {
			Calendar cal = Calendar.getInstance();
			Date date = org.apache.commons.lang3.time.DateUtils.parseDate(
					yyymmdd, CommonUtils.yyymmddFormat);
			cal.setTime(date);
			cal.add(Calendar.DAY_OF_MONTH, Integer.parseInt("-" + dayOfPeriod));

			return CommonUtils.getDate(cal.getTime());

		} catch (Exception e) {
			Log.e(TAG, "error=" + e);
		}
		return "";
	}

//	@Override
//	public List<Bill> createManyBills(Bill templateBill, String period,
//			String eachMoney) {
//
//		List<Bill> bills = new ArrayList<Bill>();
//		try {
//
//			int time = Integer.parseInt(period);
//			String yyymmdd = templateBill.getDueDate();
//			for (int i = 0; i < time; i++) {
//
//				Bill bill = this.getColonBill(templateBill);
//
//				bill.setDollar(eachMoney);
//				bill.setDueDate(yyymmdd);
//
//				yyymmdd = this.getNextDateBy(yyymmdd);
//				bills.add(bill);
//
//			}
//		} catch (Exception e) {
//			Log.e(TAG, " createManyBills error={}" + e);
//		}
//		// TODO Auto-generated method stub
//		return bills;
//	}

	@Override
	public DateRange getRangDay(final String dayOfPeriod) {
		final DateRange range = new DateRange();
		range.setBegin("0000/00/00");
		if (StringUtils.isBlank(dayOfPeriod)) {
			range.setEnd("9999/99/99");
		}

		Date paydate = CommonUtils.getDateAsDate(dayOfPeriod);// 繳費日期
		final Date nowDate = CommonUtils.getDateAsDate(CommonUtils.getDate());// 今天

		Calendar calendar = Calendar.getInstance();

		while (paydate.before(nowDate)) {
			calendar.setTime(paydate);
			calendar.add(Calendar.MONTH, +1);
			paydate = calendar.getTime();
		}
		range.setEnd(CommonUtils.getDate(paydate));

		return range;
	}
}
